﻿@using Microsoft.AspNet.Identity;
@model IEnumerable<ArticleWeb.Models.Article>
@{
    ViewBag.Title = "Index";
    var editByAnother = new Func<ArticleWeb.Models.Article, bool>(item => item.EditingBy.HasValue && item.EditingBy.ToString() != User.Identity.GetUserId());
    var cats = ((List<IDName>)ViewBag.AllCategories).ToDictionary(x => x.ID);
}

<link href="~/content/jqueryui/jquery-ui.min.css" rel="stylesheet" />
<form class="layui-form" action="" id="searchForm">
    <input type="hidden" name="size" value="@Request["size"]" />
    <input type="text" id="kw" name="kw" value="@ViewBag.Kw" />
    @if (ViewBag.ExcludeKw)
    {
        <input type="checkbox" name="excludeKw" value="True" checked="checked" lay-skin="primary" title="不包含关键词" />
    }
    else
    {
        <input type="checkbox" name="excludeKw" value="True" lay-skin="primary" title="不包含关键词" />
    } 
    <div class="layui-inline">
        <label class="layui-form-label">分类</label>
        <div class="layui-inline">
            <select name="catId1" lay-filter="catId1">
                <option>不限类别</option>
                @foreach (var item in ViewBag.Categories)
                {
                    if (Request["catId1"] == item.ID.ToString())
                    {
                        <option selected="selected" value="@item.ID">@item.Name</option>
                    }
                    else
                    {
                        <option value="@item.ID">@item.Name</option>
                    }
                }
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <select name="catId2" lay-filter="catId2">
            <option>不限类别</option>
            @foreach (var item in ViewBag.Categories2)
            {
                if (Request["catId2"] == item.ID.ToString())
                {
                    <option selected="selected" value="@item.ID">@item.Name</option>
                }
                else
                {
                    <option value="@item.ID">@item.Name</option>
                }
            }
        </select>
    </div>
    <div class="layui-inline">
        <select name="catId3" lay-filter="catId3">
            <option>不限类别</option>
            @foreach (var item in ViewBag.Categories3)
            {
                if (Request["catId3"] == item.ID.ToString())
                {
                    <option selected="selected" value="@item.ID">@item.Name</option>
                }
                else
                {
                    <option value="@item.ID">@item.Name</option>
                }
            }
        </select>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">采编状态</label><div class="layui-inline">
            <select name="state" lay-filter="state">
                <option>不限状态</option>
                @foreach (var item in EnumUtil.GetIDNames<ArticleWeb.Models.ArticleState>())
                {
                    if (this.Request.Params["state"] == item.ID.ToString())
                    {
                        <option selected="selected" value="@item.ID">@item.Name</option>
                    }
                    else
                    {
                        <option value="@item.ID">@item.Name</option>
                    }
                }
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">添加日期</label><div class="layui-btn-group">
            <button data-day="0" class="time-btn layui-btn layui-btn-primary">不限</button>
            <button data-day="1" class="time-btn layui-btn layui-btn-primary">今天</button>
            <button data-day="7" class="time-btn layui-btn layui-btn-primary">7天</button>
            <button data-day="30" class="time-btn layui-btn layui-btn-primary">30天</button>
            <button data-day="-30" class="time-btn layui-btn layui-btn-primary">本月</button>
        </div>
        <div class="layui-inline">
            <input name="time" class="layui-input" value="@Request["time"]" />
        </div>
    </div>
</form>
<button id="delList">删除本页</button>
<table class="table layui-table">
    <thead>
        <tr>
            <th>
                ID
            </th>
            <th>
                标题
            </th>
            <th>
                片段
            </th>
            <th>
                分类
            </th>
            <th>
                入库时间
            </th>
            <th>
                采编状态
            </th>
            <th>
                操作
            </th>
        </tr>
    </thead>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.ID</td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Fragments)
            </td>
            <td>
                @if (cats.ContainsKey(item.CatagoryID))
                {
                    @cats[item.CatagoryID].Name;
                }
                else
                {
                    @item.CatagoryID
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreateTime)
            </td>
            <td title="蓝色=自己正在采编；红色=他人正在采编">
                @if (item.EditingBy.HasValue)
                {
                    <span style="color:@(item.EditingBy.ToString() == User.Identity.GetUserId() ? "blue" : "red")">采编中...</span>
                }
                else if (item.State == ArticleWeb.Models.ArticleState.已采编)
                {
                    <span style="background-color:#5FB878;padding:3px;color:white">已采编</span>
                }
                else
                {
                    <span>@item.State</span>
                }
            </td>
            <td>
                @if (!editByAnother(item))
                {
                    @Html.ActionLink("采编", "Edit2", new { id = item.ID }) <text>|</text>
                    @Html.ActionLink("编辑", "Edit", new { id = item.ID,returnUrl=Request.Url.PathAndQuery }) <text>|</text>
                    @Html.ActionLink("删除", "Delete", new { id = item.ID }, new { @class = "del-btn" })
                    //<a href="javascript:;" onclick="deleteConfirm(@item.ID)">删除</a>
                }
            </td>
        </tr>
    }
</table>
@using Webdiyer.WebControls.Mvc;
@Html.Pager(new PagedList<ArticleWeb.Models.Article>(Model, ViewBag.Page, ViewBag.Size, ViewBag.Total), new PagerOptions
{
    PageIndexParameterName = "page",
    CurrentPagerItemWrapperFormatString = "<span class=\"layui-laypage-curr\"><em class=\"layui-laypage-em\"></em><em>{0}</em></span>",
}, new { @class = "layui-box layui-laypage layui-laypage-default" })
@section scripts{
    <script src="~/content/jqueryui/jquery-ui.min.js"></script>
    <script>
        Date.prototype.Format = function (fmt) { //author: meizz
            var o = {
                "M+": this.getMonth() + 1,                 //月份
                "d+": this.getDate(),                    //日
                "h+": this.getHours(),                   //小时
                "m+": this.getMinutes(),                 //分
                "s+": this.getSeconds(),                 //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds()             //毫秒
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }

        app.use('article/index');
        $('#delList').click(function () {
            if (confirm('将要删除本页数据, 是否确定删除?')) {
                $('#searchForm').append($('<input type="hidden" name="delList" value="True">'))
                $('#searchForm').submit();
            }
        });
        $('[name=exludeKw]').click(function () {
            $('.time-btn').click();
        });
        $('.time-btn').click(function () {
            var d = new Date();
            var day = $(this).data("day");
            if (day == 1) {

            }
            else if (day == 7) {
                d.setDate(d.getDate() - 7);
            }
            else if (day == 30) {
                d.setDate(d.getDate() - 30);
            }
            else if (day == -30) {
                d.setDate(1);
            }
            if (day == 0) {
                $('[name=time]').val('');
            }
            else {
                $('[name=time]').val(d.Format('yyyy-MM-dd') + '$' + new Date().Format('yyyy-MM-dd'));
            }
            $('#searchForm').submit();
        })
        //$('.layui-laypage a').click(function (e) {
        //    e.preventDefault();
        //    var href = $(this).attr('href');
        //    if (href) {
        //        var idx = href.indexOf('page=');
        //        if (idx != -1) {
        //            var page = href.substr(idx, href.length - 1)
        //            $('#page').val(page);
        //            $('#searchForm').submit();
        //        }
        //    }
        //});
        $(function () {
            $.datepicker.regional['zh-CN'] = {
                closeText: '关闭',
                prevText: '<上月',
                nextText: '下月>',
                currentText: '今天',
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'],
                monthNamesShort: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
                dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
                dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
                dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
                weekHeader: '周',
                dateFormat: 'yy-mm-dd',
                firstDay: 1,
                isRTL: false,
                showMonthAfterYear: true,
                yearSuffix: '年'
            };
            $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
            $("[name=time]").datepicker({
                showOtherMonths: true,
                selectOtherMonths: true,
                showButtonPanel: true,
                dateFormat: 'yy-M-dd$yy-M-dd',
                onSelect: function () {
                    $('#searchForm').submit();
                }
            });
        });
    </script>
}
